package cn.ygyg.bps.bus.starter.listener;

import cn.ygyg.bps.bus.starter.entity.EventTrace;
import cn.ygyg.bps.bus.starter.repository.BpsBusEventTraceRepository;
import cn.ygyg.bps.bus.starter.repository.BpsBusEventTraceRepositoryImpl;
import cn.ygyg.bps.bus.starter.service.EventTraceService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.cloud.bus.event.AckRemoteApplicationEvent;
import org.springframework.cloud.bus.event.SentApplicationEvent;
import org.springframework.cloud.bus.event.TraceListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @desc：BpsTraceListener
 * @author: guanliang.xue
 * @date: 2021/5/11 10:09
 * @version:
 * @since:
 */
public class BpsTraceListener {
    private static Log log = LogFactory.getLog(TraceListener.class);

    private BpsBusEventTraceRepository repository;

    private EventTraceService eventTraceService;

    public BpsTraceListener(BpsBusEventTraceRepository repository,EventTraceService eventTraceService) {
        this.repository = repository;
        this.eventTraceService = eventTraceService;
    }

    @EventListener
    public void onAck(AckRemoteApplicationEvent event) {
        Map<String, Object> trace = getReceivedTrace(event);
        this.repository.add(trace);
        // 落地到数据库或者日志文件

        /*EventTrace eventTrace = new EventTrace();
        eventTrace.setEventId(event.getId());
        eventTrace.setEventSignal("spring.cloud.bus.ack");
        eventTrace.setOriginService(event.getOriginService());
        eventTrace.setDestinationService(event.getDestinationService());
        eventTrace.setCreateTime(LocalDateTime.now());
        eventTrace.setUpdateTime(LocalDateTime.now());
        eventTraceService.insert(eventTrace);*/
    }

    @EventListener
    public void onSend(SentApplicationEvent event) {
        Map<String, Object> trace = getSentTrace(event);
        this.repository.add(trace);
        // todo 落地到数据库或者日志文件
    }

    protected Map<String, Object> getSentTrace(SentApplicationEvent event) {
        Map<String, Object> map = new LinkedHashMap<String, Object>();
        map.put("signal", "spring.cloud.bus.sent");
        map.put("type", event.getType().getSimpleName());
        map.put("id", event.getId());
        map.put("origin", event.getOriginService());
        map.put("destination", event.getDestinationService());
        if (log.isDebugEnabled()) {
            log.debug(map);
        }
        return map;
    }

    protected Map<String, Object> getReceivedTrace(AckRemoteApplicationEvent event) {
        Map<String, Object> map = new LinkedHashMap<String, Object>();
        map.put("signal", "spring.cloud.bus.ack");
        map.put("event", event.getEvent().getSimpleName());
        map.put("id", event.getAckId());
        map.put("origin", event.getOriginService());
        map.put("destination", event.getAckDestinationService());
        if (log.isDebugEnabled()) {
            log.debug(map);
        }
        return map;
    }
}
